package com.tgy.demo;

import com.tgy.demo.utils.JdbcUtils;

import java.util.Date;

import java.sql.*;

/***
 * @ClassName: TestPre
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 11:54 2021/11/30
 * @version : V1.0
 */
public class TestPre {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement st = null;
        PreparedStatement stDelete = null;
        PreparedStatement stUpdate = null;
        ResultSet resultSet = null;
        try {
            //得到连接
            connection = JdbcUtils.getConnection();

            //编写sql
            //区别，使用占位符代替参数
            String insertSql = "insert into users(`name`,`password`,`email`,`birthday`) values (?,?,?,?)";
            String deleteSql = "delete from users where id = ?";
            String updateSql = "update users set name = '啊哈哈哈' where id = ?";
            String querySql = "select * from users ";

            //预编译SQL，先写sql，不执行
            st = connection.prepareStatement(insertSql);
            stDelete = connection.prepareStatement(deleteSql);
            stUpdate = connection.prepareStatement(updateSql);
            //手动给参数赋值
            st.setString(1,"测试");
            st.setString(2,"666666");
            st.setString(3,"11515@eqw.com");
            // sql.Date 数据库使用
            //Util.Date Java使用   new Date().getTime()获得时间戳
            st.setDate(4,new java.sql.Date(new Date().getTime()));
            stUpdate.setInt(1,15);
            stDelete.setInt(1,15);
            //int i1 = st.executeUpdate();
            //int i1 = stUpdate.executeUpdate();
            int i1 = stDelete.executeUpdate();
            System.out.println(i1);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release(connection,st,resultSet);
        }

    }
}
